﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using DnPakExplorer.Forms;

namespace DnPakExplorer.Controls {
	public partial class GlyphCtrl : ItemSlotPanelCtrl {

        const string XML_PATH = ".\\Data\\glypgh.xml";

		ItemSlotBox2 mGlyph1;
		ItemSlotBox2 mGlyph2;
		ItemSlotBox2 mGlyph3;
		ItemSlotBox2 mGlyph4;
		ItemSlotBox2 mGlyph5;
		ItemSlotBox2 mGlyph6;
		ItemSlotBox2 mGlyph7;
		ItemSlotBox2 mGlyph8;

		ItemSlotBox2 mGlyphSkill1;
		ItemSlotBox2 mGlyphSkill2;
		ItemSlotBox2 mGlyphSkill3;
		ItemSlotBox2 mGlyphSkill4;

		ItemSlotBox2 mGlyphS1;
		ItemSlotBox2 mGlyphS2;
		ItemSlotBox2 mGlyphS3;
		ItemSlotBox2 mGlyphS4;

		ItemSlotBox2 mCashGlyph1;
		ItemSlotBox2 mCashGlyph2;
		ItemSlotBox2 mCashGlyph3;

		int mSlotWidth = 64;
		int mSlotHeight = 86;

		public GlyphCtrl() {
			InitializeComponent();


			AddSlot(mGlyph1 = new ItemSlotBox2());
			AddSlot(mGlyph2 = new ItemSlotBox2());
			AddSlot(mGlyph3 = new ItemSlotBox2());
			AddSlot(mGlyph4 = new ItemSlotBox2());
			AddSlot(mGlyph5 = new ItemSlotBox2());
			AddSlot(mGlyph6 = new ItemSlotBox2());
			AddSlot(mGlyph7 = new ItemSlotBox2());
			AddSlot(mGlyph8 = new ItemSlotBox2());
			AddSlot(mGlyphS1 = new ItemSlotBox2());
			AddSlot(mGlyphS2 = new ItemSlotBox2());
			AddSlot(mGlyphS3 = new ItemSlotBox2());
			AddSlot(mGlyphS4 = new ItemSlotBox2());
			AddSlot(mGlyphSkill1 = new ItemSlotBox2());
			AddSlot(mGlyphSkill2 = new ItemSlotBox2());
			AddSlot(mGlyphSkill3 = new ItemSlotBox2());
			AddSlot(mGlyphSkill4 = new ItemSlotBox2());
			AddSlot(mCashGlyph1 = new ItemSlotBox2());
			AddSlot(mCashGlyph2 = new ItemSlotBox2());
			AddSlot(mCashGlyph3 = new ItemSlotBox2());

			mGlyph1.Bounds = new Rectangle(10, 10, mSlotWidth, mSlotHeight);
			mGlyph1.TargetSlotId = EquipmentPanel.IS_GLYGH_NORMAL1;
			mGlyph2.Bounds = new Rectangle(70, 10, mSlotWidth, mSlotHeight);
			mGlyph2.TargetSlotId = EquipmentPanel.IS_GLYGH_NORMAL2;
			mGlyph3.Bounds = new Rectangle(130, 10, mSlotWidth, mSlotHeight);
			mGlyph3.TargetSlotId = EquipmentPanel.IS_GLYGH_NORMAL3;
			mGlyph4.Bounds = new Rectangle(190, 10, mSlotWidth, mSlotHeight);
			mGlyph4.TargetSlotId = EquipmentPanel.IS_GLYGH_NORMAL4;
			mGlyph5.Bounds = new Rectangle(250, 10, mSlotWidth, mSlotHeight);
			mGlyph5.TargetSlotId = EquipmentPanel.IS_GLYGH_NORMAL5;
			mGlyph6.Bounds = new Rectangle(310, 10, mSlotWidth, mSlotHeight);
			mGlyph6.TargetSlotId = EquipmentPanel.IS_GLYGH_NORMAL6;
			mGlyph7.Bounds = new Rectangle(10, 70, mSlotWidth, mSlotHeight);
			mGlyph7.TargetSlotId = EquipmentPanel.IS_GLYGH_NORMAL7;
			mGlyph8.Bounds = new Rectangle(70, 70, mSlotWidth, mSlotHeight);
			mGlyph8.TargetSlotId = EquipmentPanel.IS_GLYGH_NORMAL8;

			mGlyphSkill1.Bounds = new Rectangle(130, 70, mSlotWidth, mSlotHeight);
			mGlyphSkill1.TargetSlotId = EquipmentPanel.IS_GLYGH_SKILL1;
			mGlyphSkill2.Bounds = new Rectangle(190, 70, mSlotWidth, mSlotHeight);
			mGlyphSkill2.TargetSlotId = EquipmentPanel.IS_GLYGH_SKILL2;
			mGlyphSkill3.Bounds = new Rectangle(250, 70, mSlotWidth, mSlotHeight);
			mGlyphSkill3.TargetSlotId = EquipmentPanel.IS_GLYGH_SKILL3;
			mGlyphSkill4.Bounds = new Rectangle(310, 70, mSlotWidth, mSlotHeight);
			mGlyphSkill4.TargetSlotId = EquipmentPanel.IS_GLYGH_SKILL4;

			mCashGlyph1.Bounds = new Rectangle(10, 130, mSlotWidth, mSlotHeight);
			mCashGlyph1.TargetSlotId = EquipmentPanel.IS_CASH_GLYGH1;
			mCashGlyph2.Bounds = new Rectangle(70, 130, mSlotWidth, mSlotHeight);
			mCashGlyph2.TargetSlotId = EquipmentPanel.IS_CASH_GLYGH2;
			mCashGlyph3.Bounds = new Rectangle(130, 130, mSlotWidth, mSlotHeight);
			mCashGlyph3.TargetSlotId = EquipmentPanel.IS_CASH_GLYGH3;

			mGlyphS1.Bounds = new Rectangle(190, 130, mSlotWidth, mSlotHeight);
			mGlyphS1.TargetSlotId = EquipmentPanel.IS_GLYGH_S1;
			mGlyphS2.Bounds = new Rectangle(250, 130, mSlotWidth, mSlotHeight);
			mGlyphS2.TargetSlotId = EquipmentPanel.IS_GLYGH_S2;
			mGlyphS3.Bounds = new Rectangle(310, 130, mSlotWidth, mSlotHeight);
			mGlyphS3.TargetSlotId = EquipmentPanel.IS_GLYGH_S3;
			mGlyphS4.Bounds = new Rectangle(10, 190, mSlotWidth, mSlotHeight);
			mGlyphS4.TargetSlotId = EquipmentPanel.IS_GLYGH_S4;

			double scale = 0.8f;

			mSlotBackground.ImageSource = ItemSlotPanelCtrl.mSlotBackground.ImageSource;
			mSlotBackground.SourceRect = new Rectangle(0, 0, 69, 69);
			mSlotBackground.DestRect = new Rectangle(0, 0, (int)Math.Round(69*scale), (int)Math.Round(69*scale));

			this.ForAllView<ItemSlotBox>(slot => {
				slot.Background = mSlotBackground;
				slot.SeletedImage = mSelectedImage;
				slot.ItemDestRect = new Rectangle((int)Math.Round(10*scale), (int)Math.Round(10*scale), (int)Math.Round(50*scale), (int)Math.Round(50*scale));
				slot.TypeImage = GetTypeImage(slot.TargetSlotId, new Rectangle((int)Math.Round(10*scale), (int)Math.Round(10*scale), (int)Math.Round(50*scale), (int)Math.Round(50*scale)));
			});



			if (File.Exists(XML_PATH)) {
				XmlDocument doc = new XmlDocument();
				doc.Load(XML_PATH);
				XmlNodeList list = doc.SelectNodes("/Root/Slot");
				int i = 0;
				this.ForEachView<ItemSlotBox>(slot => {
					if (i<list.Count) {
						XmlAttributeCollection attrs = list[i++].Attributes;
						slot.Bounds = new Rectangle(
							int.Parse(attrs.GetNamedItem("x").InnerText),
							int.Parse(attrs.GetNamedItem("y").InnerText),
							slot.Bounds.Width,
							slot.Bounds.Height
							);
						return false;
					} else
						return true;
				});
			}

		}


		protected override void SaveLayout() {
			XmlDocument doc = new XmlDocument();
			doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", ""));
			XmlElement rootNode = doc.CreateElement("Root");
			doc.AppendChild(rootNode);
			this.ForAllView<ItemSlotBox>(slot => {
				XmlElement slotNode = doc.CreateElement("Slot");
				slotNode.SetAttribute("x", slot.Bounds.X.ToString());
				slotNode.SetAttribute("y", slot.Bounds.Y.ToString());
				rootNode.AppendChild(slotNode);
			});
			doc.Save(XML_PATH);
		}
	}
}
